home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 10586 / 10586.xpi / chrome / content / tinyurl.js < prev   
Text File  |  2009-03-13  |  8KB  |  284 lines

  1. const clipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); 
  2.  
  3. var tinyurlgen = {
  4.     
  5.     //Is the current page being processed
  6.     processing : false,
  7.     //The XMLHTTPRequest Object
  8.     request : null,
  9.     //Does the link need to be previewable?
  10.     preview : false,
  11.     //TinyURL options
  12.     settings : {
  13.         createpreviewbydefault : false,
  14.         showoption : true
  15.         },
  16.     //TinyURL localised strings object
  17.     stringsobj : null,
  18.     //A reference to the object that was context clicked
  19.     contextobj : null,
  20.     
  21.     //A function to copy the returned URL to the clipboard
  22.     copytoclipboard : function(text){
  23.         
  24.         //Copy text to clipboard
  25.         clipboard.copyString(text);
  26.         },
  27.     
  28.     //A function to ease Component Class interfacing
  29.     cc : function(cName, ifaceName){
  30.         
  31.         return Components.classes[cName].getService(Components.interfaces[ifaceName]);
  32.         },
  33.     
  34.     init : function(){
  35.         //Get the broswer XUL Object
  36.         var appcontent = document.getElementById("appcontent");   // browser
  37.         //If it exists...
  38.         if(appcontent){
  39.             //Attach a listener that resets the processing state as a new page is loaded
  40.             appcontent.addEventListener("DOMContentLoaded", tinyurlgen.reset, false);
  41.             }
  42.         //Attach listener event to context menu to show and hide menuitem
  43.         var contextMenu = document.getElementById("contentAreaContextMenu");
  44.         if(contextMenu){
  45.             contextMenu.addEventListener("popupshowing", function(){
  46.                 var item = document.getElementById('tinyurlgen-context-option');
  47.                 var hidden = true;
  48.                 var el;
  49.                 if(document.popupNode && tinyurlgen.settings.showoption){
  50.                     el = document.popupNode;
  51.                     while(el){
  52.                         if(el.localName == 'A'){
  53.                             if(el.href != ''){
  54.                                 tinyurlgen.contextobj = el;
  55.                                 hidden = false;
  56.                                 }
  57.                             break;
  58.                             }
  59.                         else{
  60.                             el = el.parentNode;
  61.                             }
  62.                         }
  63.                     }
  64.                 
  65.                 
  66.                 item.hidden = hidden;
  67.                 }, false);
  68.             }
  69.         
  70.         //Load and apply settings
  71.         tinyurlgen.applysettings();
  72.         },
  73.     
  74.     applysettings : function(){
  75.         
  76.         //Load settings from config
  77.         var prefManager = Components.classes["@mozilla.org/preferences-service;1"]
  78.                             .getService(Components.interfaces.nsIPrefBranch);
  79.         
  80.         tinyurlgen.settings.createpreviewbydefault = prefManager.getBoolPref("extensions.tinyurlgen.createpreviewbydefault");
  81.         tinyurlgen.settings.showoption = prefManager.getBoolPref("extensions.tinyurlgen.showcontextoption");        
  82.         
  83.         //Get button references
  84.         var create = document.getElementById('tinyurlgen-button-create');
  85.         var createwithpreview = document.getElementById('tinyurlgen-button-create-with-preview');
  86.         var contextitem = document.getElementById('tinyurlgen-context-option');
  87.         //If Create Previewable link by default...
  88.         if(tinyurlgen.settings.createpreviewbydefault){
  89.             //Set to default
  90.             createwithpreview.setAttribute("default", true);
  91.             //Unset default
  92.             create.setAttribute("default", "false");
  93.             //Move option to top
  94.             createwithpreview.parentNode.insertBefore(createwithpreview, create);
  95.             //Update context option label
  96.             contextitem.setAttribute("label", tinyurlgen.string("label.generatewithpreview"));
  97.             }
  98.         else{
  99.             //Set to default
  100.             create.setAttribute("default", true);
  101.             //Unset default
  102.             createwithpreview.setAttribute("default", "false");
  103.             //Move option to top
  104.             create.parentNode.insertBefore(create, createwithpreview);
  105.             //Update context option label
  106.             contextitem.setAttribute("label", tinyurlgen.string("label.generate"));
  107.             }
  108.         },
  109.     
  110.     //A function to generate a TinyURL
  111.     generate : function(evt){
  112.         
  113.         //If the icon was 'right clicked', end execution
  114.         if(evt.button == 2){
  115.             return false;
  116.             }
  117.         
  118.         //If Create Previewable link by default
  119.         if(tinyurlgen.settings.createpreviewbydefault){
  120.             //Set preview to true
  121.             tinyurlgen.preview = true;
  122.             }
  123.         
  124.         //If Create Previwable link option clicked
  125.         if(evt.target.id == 'tinyurlgen-button-create-with-preview'){
  126.             //Set preview to true
  127.             tinyurlgen.preview = true;
  128.             }
  129.         
  130.         //If the page is already being process, end execution
  131.         if(tinyurlgen.processing){
  132.             return false;
  133.             }
  134.         
  135.         if(evt.target.id == 'tinyurlgen-context-option'){
  136.             //Get linked page URL
  137.             var url = tinyurlgen.contextobj.href;
  138.             tinyurlgen.contextobj = null;
  139.             }
  140.         else{
  141.             //Get current page URL
  142.             var url = content.document.location.href;
  143.             }
  144.         
  145.         //Encode URL
  146.         url = encodeURIComponent(url);
  147.         
  148.         //Set state to processing
  149.         tinyurlgen.setstate('processing');
  150.     
  151.         //Create XMLHTTPRequest Object
  152.         tinyurlgen.request = new XMLHttpRequest();
  153.         //Set onload handler
  154.         tinyurlgen.request.onload = tinyurlgen.handleresponse;
  155.         //Constuct request
  156.         tinyurlgen.request.open("GET", "http://tinyurl.com/api-create.php?url=" + url, true);
  157.         //Send request
  158.         tinyurlgen.request.send(null);
  159.         },
  160.     
  161.     handleresponse : function(){
  162.         
  163.         //Collect XMLHTTPRequest response
  164.         var response = tinyurlgen.request.responseText;
  165.         //Destroy XMLHTTPRequest Object
  166.         tinyurlgen.request = null;
  167.         
  168.         //Check that the response is valid
  169.         if(response.match(/http\:\/\/tinyurl.com\//gi)){
  170.             //Is this a previewable link?
  171.             if(tinyurlgen.preview){
  172.                 //Make link previewable
  173.                 response = response.replace(/http\:\/\/tinyurl.com\//gi, 'http://preview.tinyurl.com/')
  174.                 }
  175.             //Set current state (complete)
  176.             tinyurlgen.setstate('complete');
  177.             //Copy URL to clipboard
  178.             tinyurlgen.copytoclipboard(response);
  179.             //Reset state in 5 seconds
  180.             setTimeout('tinyurlgen.reset();', 5000);
  181.             }
  182.         else{
  183.             //Set current state (error)
  184.             tinyurlgen.setstate('error');
  185.             setTimeout('tinyurlgen.reset();', 5000);
  186.             }
  187.         },
  188.     
  189.     reset : function(){
  190.         
  191.         //If a TinyURL is not being generated
  192.         if(!tinyurlgen.processing){
  193.             //Reset the icon
  194.             tinyurlgen.setstate(null);
  195.             }
  196.         },
  197.     
  198.     setstate : function(state){
  199.         
  200.         //Define variables
  201.         var icon;
  202.         var title, src, processing;
  203.         
  204.         //Get relevant titles and icons
  205.         switch(state){
  206.             //TinyURL retrieved successfully
  207.             case 'complete':
  208.                 src = 'okay.png';
  209.                 title = tinyurlgen.string('state.complete');
  210.                 processing = false;
  211.                 break;
  212.             
  213.             //Error occurred when retrieving TinyURL
  214.             case 'error':
  215.                 src = 'error.png';
  216.                 title = tinyurlgen.string('state.error');
  217.                 processing = false;
  218.                 break;
  219.             
  220.             //Currently retrieving TinyURL
  221.             case 'processing':
  222.                 src = 'process.gif';
  223.                 title = tinyurlgen.string('state.processing');
  224.                 processing = true;
  225.                 break;
  226.             
  227.             //Reset to default state
  228.             default:
  229.                 src = 'link.png';
  230.                 title = tinyurlgen.string('state.default');
  231.                 processing = false;
  232.                 break;            
  233.             }
  234.         
  235.         //Reset processing state
  236.         tinyurlgen.processing = processing;
  237.         
  238.         //If processing complete, reset 'preview' flag
  239.         if(!processing){
  240.             tinyurlgen.preview = false;
  241.             }
  242.         
  243.         //Get icon object
  244.         icon = document.getElementById('tinyurlgen-status-bar-icon');
  245.         if(icon){
  246.             icon.setAttribute('src', 'chrome://tinyurlgen/skin/' + src);
  247.             icon.setAttribute('tooltiptext', title);
  248.             }
  249.         },
  250.     
  251.     showoptions : function(){
  252.         
  253.         //Get reference to window object
  254.         var win = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  255.                       .getService(Components.interfaces.nsIWindowMediator)
  256.                       .getMostRecentWindow("TinyURL:Options");
  257.         //If window is already open...
  258.         if(win){
  259.             //Focus it
  260.             win.focus();
  261.             }
  262.         //If not
  263.         else{
  264.             //Create new dialog window
  265.             var parentWindow = (!window.opener || window.opener.closed) ? window : window.opener;
  266.             parentWindow.openDialog("chrome://tinyurlgen/content/options.xul", "_blank", "resizable=no,dialog=no,centerscreen");
  267.             }
  268.         },
  269.     
  270.     string : function(stringname){
  271.         
  272.         if(!tinyurlgen.stringobj){
  273.             var localeService = this.cc("@mozilla.org/intl/nslocaleservice;1", "nsILocaleService");
  274.             var bundleService = this.cc("@mozilla.org/intl/stringbundle;1", "nsIStringBundleService");
  275.             var appLocale = localeService.getApplicationLocale();
  276.             tinyurlgen.stringobj = bundleService.createBundle("chrome://tinyurlgen/locale/tinyurl.properties", appLocale);
  277.             }
  278.         
  279.         return tinyurlgen.stringobj.GetStringFromName(stringname);
  280.         }
  281.     }
  282.  
  283. //Reset state as new page loads
  284. window.addEventListener('load', tinyurlgen.init, false);